<?php

/**
 * Utils class, define a lib of utility functions.
 * @static
 * @package lib
 * @author alcaitiff
 */

class Utils
{

    /**
     * Return true if $v can be parsed as an integer greater than zero
     * @static
     * @param string|integer|float $v Value to test
     * @return boolean
     */
	public static function isIntegerGreaterThanZero($v){
		$i=((int)$v);
		if ( $i==$v && $i>0)
			return true;
		else
			return false;
	}

	/**
     * Returns an array with the elements that starts with the given needle
     * @static
     * @param array $array An initial array
     * @param string $needle A value to search
     * @param int $max The max of results in the result
     * @return array
     */
	public static function arraySearch($array,$needle,$max=null){
		$ret=array();
		$found=0;
		foreach($array as $key=>$value){
			if(stripos($value,$needle)===0){
				$ret[$key]=$value;
				$found++;
				if($max>0 && $found>=$max){
				break;
				}
			}
		}
		return $ret;
	}
	/**
     * Return true if the string start with the needle
     * @static
     * @param string $string The string base
     * @param string $needle The needle to search
     * @return boolean
     */
	public static function startWith($string, $needle){
		if(stripos($string,$needle)===0)
			return true;
		else
			return false;
	}
    /**
     * Remove null and '' values from an array
     * @param array $array
     * @param bool $preserveKeys
     * @return array
     */
    public static function removeArrayNullValues($array,$preserveKeys=FALSE){
        $ret=array();
        if(!is_array($array)){
            return $ret;
        }else{
            foreach($array as $key=>$value){
                if($value!==null && $value!==''){
                    if($preserveKeys){
                        $ret[$key]=$value;
                    }else{
                        $ret[]=$value;
                    }
                }
            }
        }
        return $ret;
    }
}